<template>
	<view class="container">
		<view class="form-container">
			<!-- 标题 -->
			<view class="title">修改密码</view>

			<!-- 旧密码 -->
			<view class="form-item">
				<u-icon name="lock" size="24" color="#409EFF" class="icon"></u-icon>
				<u-input v-model="oldPassword" type="password" placeholder="请输入旧密码" class="input" />
			</view>

			<!-- 新密码 -->
			<view class="form-item">
				<u-icon name="lock-open" size="24" color="#55aa00" class="icon"></u-icon>
				<u-input v-model="newPassword" type="password" placeholder="请输入新密码" class="input" />
			</view>

			<!-- 确认新密码 -->
			<view class="form-item">
				<u-icon name="checkmark-circle" size="24" color="#67C23A" class="icon"></u-icon>
				<u-input v-model="confirmPassword" type="password" placeholder="请再次输入新密码" class="input" />
			</view>

			<!-- 提交按钮 -->
			<view class="button-container">
				<u-button type="primary" shape="circle" @click="submitPasswordChange">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				oldPassword: "",
				newPassword: "",
				confirmPassword: "",
			};
		},
		methods: {
			async submitPasswordChange() {
				if (this.newPassword !== this.confirmPassword) {
					uni.showToast({
						title: "两次输入的新密码不一致",
						icon: "none",
					});
					return;
				}

				// 调用接口修改密码
				await uni.request({
					url: `${this.$baseUrl}/user/changePassword`,
					method: "POST",
					header: {
						Authorization: "Bearer " + uni.getStorageSync("access_token"),
						"Content-Type": "application/json",
					},
					data: {
						oldPassword: this.oldPassword,
						newPassword: this.newPassword,
					},
				}).then((data) => {
					var [err, res] = data;
					if (res.data.changeSucceed) {
						uni.showToast({
							title: "密码修改成功",
							icon: "success",
							duration: 1500, // 提示显示时长
							success: () => {
								// 延迟跳转，确保用户看到提示
								setTimeout(() => {
									uni.navigateBack(); // 返回上一页
								}, 1500); // 延迟时间与提示显示时长一致
							},
						});

					} else {
						uni.showToast({
							title: "密码修改失败",
							icon: "none",
						});
					}
				});	
			},
		},
	};
</script>

<style lang="scss">
	.container {
		padding: 40rpx;
		background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
		min-height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.form-container {
		background-color: #fff;
		border-radius: 24rpx;
		padding: 60rpx 40rpx;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
		width: 100%;
		max-width: 600rpx;
	}

	.title {
		font-size: 40rpx;
		font-weight: bold;
		color: #333;
		text-align: center;
		margin-bottom: 60rpx;
	}

	.form-item {
		display: flex;
		align-items: center;
		margin-bottom: 40rpx;
		border-bottom: 2rpx solid #eee;
		padding-bottom: 20rpx;

		.icon {
			margin-right: 20rpx;
		}

		.input {
			flex: 1;
			font-size: 32rpx;
		}
	}

	.button-container {
		margin-top: 60rpx;

		.u-button {
			background: linear-gradient(135deg, #409EFF, #67C23A);
			color: #fff;
			font-size: 32rpx;
			height: 80rpx;
			line-height: 80rpx;
		}
	}
</style>